package worker

import (
	"chinasoccer/models"
	"chinasoccer/pkg/app"
	"chinasoccer/pkg/e"

	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

// 获取所有员工的控制器
func GetAllWorkers(c *gin.Context) {
	// 获取前端发送来的参数
	pagenum := -1
	if arg := c.Query("pagenum"); arg != "" {
		pagenum = com.StrTo(arg).MustInt()
	}
	pagesize := -1
	if arg := c.Query("pagesize"); arg != "" {
		pagesize = com.StrTo(arg).MustInt()
	}
	query := c.Query("query")
	workerParam := map[string]interface{}{
		"pagenum":  pagenum,
		"pagesize": pagesize,
		"query":    query,
	}

	info, total, err := models.GetAllWorkers(workerParam)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total, "query": query, "pagenum": pagenum, "pagesize": pagesize}, "OK")
}

// 增加员工的控制器
func AddWorker(c *gin.Context) {
	info, total, err := models.AddWorker(c)
	if total != 0 {
		app.Error(c, e.ERROR_UPLOAD_CHECK_IMAGE_FAIL, err, "该员工已经存在")
		return
	}
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, info, "OK")
}

// 更新员工的控制器
func UpdateWorker(c *gin.Context) {
	info, err := models.UpdateWorker(c)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, info, "OK")
}

// 删除员工的控制器
func DelWorker(c *gin.Context) {
	err := models.DelWorker(c)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, "worker deleted successfully", "OK")
}

// 得到员工的个人信息
func GetDoc(c *gin.Context) {
	info, err := models.GetDoc(c)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, info, "OK")
}
